package com.hrm.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.hrm.dao.ActionMapper;
import com.hrm.dao.ExtendsMapper;
import com.hrm.dao.MenuMapper;
import com.hrm.dao.RoleActionMapper;
import com.hrm.entity.Action;
import com.hrm.entity.Menu;
import com.hrm.service.MenuService;
import com.hrm.util.Page;

public class MenuServiceImpl implements MenuService {
	private static final Log log = LogFactory.getLog(MenuServiceImpl.class); 
	
	@Autowired
	private RoleActionMapper roleActionMapper;
	
	public RoleActionMapper getRoleActionMapper() {
		return roleActionMapper;
	}

	public void setRoleActionMapper(RoleActionMapper roleActionMapper) {
		this.roleActionMapper = roleActionMapper;
	}

	@Autowired
	private ActionMapper actionMapper;
	public ActionMapper getActionMapper() {
		return actionMapper;
	}

	public void setActionMapper(ActionMapper actionMapper) {
		this.actionMapper = actionMapper;
	}

	@Autowired
	private MenuMapper menuMapper;
	
	public MenuMapper getMenuMapper() {
		return menuMapper;
	}

	public void setMenuMapper(MenuMapper menuMapper) {
		this.menuMapper = menuMapper;
	}

	@Autowired
	private ExtendsMapper extendsMapper;
	
	public ExtendsMapper getExtendsMapper() {
		return extendsMapper;
	}

	public void setExtendsMapper(ExtendsMapper extendsMapper) {
		this.extendsMapper = extendsMapper;
	}

	public List<Menu> getMenus(Page page) {
		log.info("****** Do method getMenus(Page page) begin,params info ==== [page="+page+"]");
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("currentStart", page.getCurrentStart()-1);
		map.put("currentEnd", page.getCurrentStart()+page.getPagesize()-1);
		List<Menu> menus = menuMapper.selectMenuByMap(map);
		log.info("****** Do method getMenus(Page page) end,return info ==== [ List<Menu>="+menus+" ]");
		return menus;
	}

	public List<Menu> getMenus() {
		log.info("****** Do method getMenus() begin,no params info.");
		List<Menu> menus = menuMapper.selectAllMenu();
		log.info("****** Do method getMenus() end,return info ==== [ List<Menu>="+menus+" ]");
		return menus;
	}

	public Menu getMenuById(long id) {
		log.info("****** Do method getMenuById(long id) begin,params info ==== [ id="+id+" ]");
		Menu menu =  menuMapper.selectMenuById(id);
		log.info("****** Do method getMenuById(long id) end,return info ==== [ Menu="+menu+" ]");
		return menu;
	}

	public int addMenu(Menu menu) {
		log.info("****** Do method addMenu(Menu menu) begin,params info ==== [ menu="+menu+" ]");
		int cnts=-1;
		cnts= menuMapper.insertMenu(menu);
		log.info("****** Do method addMenu(Menu menu) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public int updateMenu(Menu menu) {
		log.info("****** Do method updateMenu(Menu menu) begin,params info ==== [ menu="+menu+" ]");
		int cnts=-1;
		cnts= menuMapper.updateMenu(menu);
		log.info("****** Do method updateMenu(Menu menu) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public int deleteMenuById(long id) {
		log.info("****** Do method deleteMenuById(long id) begin,params info ==== [ id="+id+" ]");
		int cnts=-1;
		cnts= menuMapper.deleteMenuById(id);
		log.info("****** Do method deleteMenuById(long id) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}


	public List<Menu> getMenusByRoleId(long roleId) {
		log.info("****** Do method getMenusByRoleId(long roleId) begin,params info ==== [ roleId="+roleId+" ]");
		List<Menu> menus=extendsMapper.getMenusByRoleId(roleId);
		for (Menu menu : menus) {
			List<Action> actions = extendsMapper.getActionsByRoleIdAndMenuId(roleId, menu.getId());
			menu.setActions(actions);
		}
		log.info("****** Do method getMenusByRoleId(long roleId) end,return info ==== [ List<Menu>="+menus+" ]");
		return menus;
	}


	public int addAction(Action action) {
		log.info("****** Do method addAction(Action action) begin,params info ==== [ action="+action+" ]");
		int cnts=-1;
		cnts= actionMapper.insertAction(action);
		log.info("****** Do method addAction(Action action) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public Action getActionById(long id) {
		log.info("****** Do method getActionById(long id) begin,params info ==== [ id="+id+" ]");
		Action action = actionMapper.selectActionById(id);
		log.info("****** Do method getActionById(long id) end,return info ==== [ Action="+action+" ]");
		return action;
	}

	public int updateAction(Action action) {
		log.info("****** Do method updateAction(Action action) begin,params info ==== [ action="+action+" ]");
		int cnts=-1;
		cnts= actionMapper.updateAction(action);
		log.info("****** Do method updateAction(Action action) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}
	@Transactional
	public int deleteActionById(long id) {
		log.info("****** Do method deleteActionById(long id) begin,params info ==== [ id="+id+" ]");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("actionId", id);
		roleActionMapper.deleteRoleActionByMap(map);
		log.info("delete all role_action records where action's id = "+id);
		int cnts=-1;
		cnts=actionMapper.deleteActionById(id);
		log.info("delete an action record where action's id = "+id);
		log.info("****** Do method deleteActionById(long id) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}
	@Transactional
	public int hideMenuById(long id) {
		log.info("****** Do method hideMenuById(long id) begin,params info ==== [ id="+id+" ]");
		Menu menu = menuMapper.selectMenuById(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("menuId", id);
		List<Action> actions = actionMapper.selectActionByMap(map);
		for(Action a :actions){
			a.setViewMode("N");
			actionMapper.updateAction(a);
			log.info("update an action record set view_mode='N' where action's id = "+id);
		}
		menu.setViewMode("N");
		int cnts=-1;
		cnts=menuMapper.updateMenu(menu);
		log.info("update a menu record where menu = "+menu);
		log.info("****** Do method hideMenuById(long id) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}
	@Transactional
	public int showMenuById(long id) {
		log.info("****** Do method showMenuById(long id) begin,params info ==== [ id="+id+" ]");
		Menu menu = menuMapper.selectMenuById(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("menuId", id);
		List<Action> actions = actionMapper.selectActionByMap(map);
		for(Action a :actions){
			a.setViewMode("Y");
			actionMapper.updateAction(a);
			log.info("update an action record set view_mode='Y' where action's id = "+id);
		}
		menu.setViewMode("Y");
		int cnts=-1;
		cnts=menuMapper.updateMenu(menu);
		log.info("update a menu record where menu = "+menu);
		log.info("****** Do method showMenuById(long id) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}



}
